package com.googlecode.microanalyst.designer.model;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.Image;

import com.googlecode.microanalyst.designer.model.property.PropertyHandler;

public class AnalysisConnection extends AnalysisElement {

	private static final long serialVersionUID = 1L;

	private static Image ICON = createImage(AnalysisDiagram.class,
			"icons/connection16.gif"); //$NON-NLS-1$

	protected AnalysisElement source, target;
	protected String sourceTerminal, targetTerminal;

	private List<AnalysisConnectionBendpoint> bendpoints = new ArrayList<AnalysisConnectionBendpoint>();

	public void attachSource() {
		AnalysisElement source = getSource();
		if (source == null || source.getSourceConnections().contains(this))
			return;
		source.connectOutput(this);
	}

	public void attachTarget() {
		AnalysisElement target = getTarget();
		if (target == null || target.getTargetConnections().contains(this))
			return;
		target.connectInput(this);
	}

	public void detachSource() {
		AnalysisElement source = getSource();
		if (source == null)
			return;
		source.disconnectOutput(this);
	}

	public void detachTarget() {
		AnalysisElement target = getTarget();
		if (target == null)
			return;
		target.disconnectInput(this);
	}

	public List<AnalysisConnectionBendpoint> getBendpoints() {
		return bendpoints;
	}

	public AnalysisElement getSource() {
		return source;
	}

	public String getSourceTerminal() {
		return sourceTerminal;
	}

	public AnalysisElement getTarget() {
		return target;
	}

	public String getTargetTerminal() {
		return targetTerminal;
	}

	public void setSource(AnalysisElement source) {
		Object old = source;
		this.source = source;
		firePropertyChange("source", old, this.source);//$NON-NLS-1$
	}

	public void setTarget(AnalysisElement target) {
		Object old = target;
		this.target = target;
		firePropertyChange("target", old, this.target);//$NON-NLS-1$
	}

	public void setSourceTerminal(String sourceTerminal) {
		Object old = sourceTerminal;
		this.sourceTerminal = sourceTerminal;
		firePropertyChange("sourceTerminal", old, this.sourceTerminal);//$NON-NLS-1$
	}

	public void setTargetTerminal(String targetTerminal) {
		Object old = targetTerminal;
		this.targetTerminal = targetTerminal;
		firePropertyChange("targetTerminal", old, this.targetTerminal);//$NON-NLS-1$
	}

	public void setBendpoints(List<AnalysisConnectionBendpoint> bendpoints) {
		Object old = bendpoints;
		this.bendpoints = bendpoints;
		firePropertyChange("bendpoint", old, this.bendpoints);//$NON-NLS-1$
	}

	public void insertBendpoint(int index, AnalysisConnectionBendpoint point) {
		getBendpoints().add(index, point);
		firePropertyChange("bendpoint", null, null);//$NON-NLS-1$
	}

	public void removeBendpoint(int index) {
		getBendpoints().remove(index);
		firePropertyChange("bendpoint", null, null);//$NON-NLS-1$
	}

	public void setBendpoint(int index, AnalysisConnectionBendpoint point) {
		getBendpoints().set(index, point);
		firePropertyChange("bendpoint", null, null);//$NON-NLS-1$
	}

	public Object clone() {
		AnalysisConnection clone = new AnalysisConnection();
		clone.source = source;
		clone.sourceTerminal = sourceTerminal;
		clone.target = target;
		clone.targetTerminal = targetTerminal;
		clone.bendpoints = bendpoints;
		return clone;
	}

	@Override
	public Image getIconImage() {
		return ICON;
	}

	public void setValues(AnalysisConnection connection) {
	}

	@Override
	public PropertyHandler getPropertyHandler() {
		return null;
	}

}
